home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 008 / vincent.arc / VINC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1979-12-31  |  11.4 KB  |  429 lines

  1. /* VINC                                                              */
  2. /*                                                                   */
  3. /*      This is a C language subroutine to display a screen and      */
  4. /*      get users data entered on it into a file as ASCII text.      */
  5. /*                                                                   */
  6.  
  7. #define NULL '\000'
  8. #define BLANK '\040'
  9. #define ESCAPE '\033'
  10. #include <stdio.h>
  11.  
  12. main(argc,argv)
  13.   int argc;
  14.   char *argv[];
  15.   {
  16.    unsigned char scrname[67],buffer[1025],dflname[67],cdata[82];
  17.    unsigned char dataname[67],filename[67],colors[82];
  18.    unsigned char dattr[101],dcolors[101][16];
  19.    int dcol[101],dline[101],dlen[101];
  20.    int filein,i,j,count,k;
  21.  
  22.    strcpy(filename,argv[1]);
  23.    strcpy(scrname,filename);
  24.    strcat(scrname,".scr");
  25.    strcpy(dflname,filename);
  26.    strcat(dflname,".dfd");
  27.    strcpy(dataname,filename);
  28.    strcat(dataname,".dat");
  29.  
  30.    ansicol("low white black",buffer) ;
  31.    ansimvc(1,1);
  32.    printf("%s",buffer) ;
  33.  
  34.    /* display user screen */
  35.    if ((filein=open(scrname,AREAD)) < 1)
  36.        abort("\nERROR READING <%s> ",scrname) ;
  37.    for(i=1;i<=24;i++)
  38.        {
  39.          if ((j=read(filein,buffer,1024)) == 0) break;
  40.          buffer[j-1] = NULL ;
  41.          ansimvc(i,1) ;
  42.          for(k=0;k<=strlen(buffer)-1;k++)
  43.             if (buffer[k] == '@') printf(" ");
  44.             else printf("%c",buffer[k]);
  45.                    /***** printf("%s",buffer);****/
  46.        }
  47.    close(filein) ;
  48.  
  49.    /* load dfd (data field definitions */
  50.    count = -1;
  51.    if ((filein=open(dflname,AREAD)) < 1)
  52.        abort("\nERROR READING <%s> ",dflname) ;
  53.    for(i=1;i<=100;i++)
  54.        {
  55.          if ((j=read(filein,buffer,1024)) == 0) break;
  56.          buffer[j-1] = NULL ;
  57.          for(j=0,k=0;j<=3;j++)
  58.             {
  59.              if ((buffer[j] == NULL) || (buffer[j] == BLANK))break;
  60.              cdata[k++]=buffer[j];
  61.              buffer[j] = BLANK ;
  62.             }
  63.          cdata[k]=NULL ;
  64.          dline[++count] = atoi(cdata) ;
  65.  
  66.          leftadj(buffer);
  67.          for(j=0,k=0;j<=3;j++)
  68.             {
  69.              if ((buffer[j] == NULL) || (buffer[j] == BLANK))break;
  70.              cdata[k++]=buffer[j];
  71.              buffer[j] = BLANK ;
  72.             }
  73.          cdata[k]=NULL ;
  74.          dcol[count] = atoi(cdata) ;
  75.  
  76.          leftadj(buffer);
  77.          for(j=0,k=0;j<=3;j++)
  78.             {
  79.              if ((buffer[j] == NULL) || (buffer[j] == BLANK))break;
  80.              cdata[k++]=buffer[j];
  81.              buffer[j] = BLANK ;
  82.             }
  83.          cdata[k]=NULL ;
  84.          dlen[count] = atoi(cdata) ;
  85.  
  86.          leftadj(buffer);
  87.          for(j=0,k=0;j<=3;j++)
  88.             {
  89.              if ((buffer[j] == NULL) || (buffer[j] == BLANK))break;
  90.              cdata[k++]=buffer[j];
  91.              buffer[j] = BLANK ;
  92.             }
  93.          cdata[k]=NULL ;
  94.          k = atoi(cdata) ;
  95.          dattr[count] = k;
  96.          makecol(dattr[count],colors);
  97.          ansicol(colors,buffer);
  98.          strcpy(dcolors[count],buffer) ;
  99.          ansimvc(dline[count],dcol[count]);
  100.          printf("%s",buffer);
  101.          for(k=1;k<=dlen[count];k++)
  102.             printf(" ");
  103.        }
  104.    close(filein) ;
  105.  
  106.    /* get users data typed in */
  107.  
  108.    if ((filein=creat(dataname,AWRITE)) < 1)
  109.        abort("\nERROR CREATING <%s> ",dataname) ;
  110.    for (i=0;i<=count;i++)
  111.       {
  112.        ansimvc(dline[i],dcol[i]);
  113.        getdata(dline[i],dcol[i],dlen[i],dcolors[i],cdata) ;
  114.        write(filein,cdata,strlen(cdata));
  115.       }
  116.    close(filein) ;
  117.    ansicol("low white black",buffer) ;
  118.    ansimvc(1,1);
  119.    printf("%s",buffer) ;
  120.    return(0) ;
  121. }
  122.  
  123. /*  ANSIMVC - MOVE CURSOR TO LINE AND COLUMN       */
  124. ansimvc(line,col)
  125.   int line,col ;
  126.   {
  127.    unsigned char cline[3],ccol[3];
  128.  
  129.    itoa(line,cline);
  130.    itoa(col,ccol);
  131.    printf("%c[%s;%sH",ESCAPE,cline,ccol);
  132.    return(0) ;
  133.   }
  134.  
  135. /* ANSICOL - convert string of color specs to escape sequences */
  136. ansicol(inbuf,colors)
  137.   unsigned char inbuf[],colors[];
  138.   {
  139.     int i,j ;
  140.     unsigned char buffer[256],colors1[30],colors2[30];
  141.     unsigned char colors0[30],colbuf[30];
  142.  
  143.     for (i=0;i<=strlen(inbuf)-1;buffer[i]=toupper(inbuf[i]),i++);
  144.     buffer[i]=NULL;
  145.     colors[0] = NULL ;
  146.     colors0[0] = NULL ;
  147.     colors1[0] = NULL ;
  148.     colors2[0] = NULL ;
  149.     colbuf[0] = NULL ;
  150.  
  151.  
  152.     /* check for monochrome verbs first */
  153.   inten: ;
  154.     leftadj(buffer);
  155.     if (strncmp(buffer,"LOW",3) == 0)
  156.        {
  157.           for(i=0;i<=2;buffer[i++]=BLANK) ;
  158.           sprintf(colors0,"%c%s",ESCAPE,"[0m");
  159.           strcat(colbuf,colors0); goto inten;
  160.        }
  161.     else
  162.     if (strncmp(buffer,"HIGH",4) == 0)
  163.       {
  164.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  165.        sprintf(colors0,"%c%s",ESCAPE,"[1m");
  166.        strcat(colbuf,colors0); goto inten;
  167.       }
  168.     else
  169.     if (strncmp(buffer,"UNDER",5) == 0)
  170.       {
  171.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  172.        sprintf(colors0,"%c%s",ESCAPE,"[4m");
  173.        strcat(colbuf,colors0); goto inten;
  174.       }
  175.     else
  176.     if (strncmp(buffer,"BLINK",5) == 0)
  177.       {
  178.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  179.        sprintf(colors0,"%c%s",ESCAPE,"[5m");
  180.        strcat(colbuf,colors0); goto inten;
  181.       }
  182.     else
  183.     if (strncmp(buffer,"REVERSE",7) == 0)
  184.       {
  185.        for(i=0;i<=6;buffer[i++]=BLANK) ;
  186.        sprintf(colors0,"%c%s",ESCAPE,"[7m");
  187.        strcat(colbuf,colors0); goto inten;
  188.       }
  189.     else
  190.     if (strncmp(buffer,"DARK",4) == 0)
  191.       {
  192.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  193.        sprintf(colors0,"%c%s",ESCAPE,"[8m");
  194.        strcat(colbuf,colors0); goto inten;
  195.       }
  196.     strcpy(colors0,colbuf) ;
  197.  
  198.     leftadj(buffer) ;
  199.     if (buffer[0] == NULL) goto exitit;
  200.  
  201.     /* default to white on black if no colors supplied */
  202.     sprintf(colors1,"%c%s",ESCAPE,"[37m");
  203.     sprintf(colors2,"%c%s",ESCAPE,"[40m");
  204.  
  205.  
  206.     /* real first color, not monochrome crt */
  207.     if (strncmp(buffer,"BLACK",5) == 0)
  208.       {
  209.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  210.        sprintf(colors1,"%c%s",ESCAPE,"[30m");
  211.       }
  212.     else
  213.     if (strncmp(buffer,"RED",3) == 0)
  214.       {
  215.        for(i=0;i<=2;buffer[i++]=BLANK) ;
  216.        sprintf(colors1,"%c%s",ESCAPE,"[31m");
  217.       }
  218.     else
  219.     if (strncmp(buffer,"GREEN",5) == 0)
  220.       {
  221.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  222.        sprintf(colors1,"%c%s",ESCAPE,"[32m");
  223.       }
  224.     else
  225.     if (strncmp(buffer,"YELLOW",6) == 0)
  226.       {
  227.        for(i=0;i<=5;buffer[i++]=BLANK) ;
  228.        sprintf(colors1,"%c%s",ESCAPE,"[33m");
  229.       }
  230.     else
  231.     if (strncmp(buffer,"BLUE",4) == 0)
  232.       {
  233.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  234.        sprintf(colors1,"%c%s",ESCAPE,"[34m");
  235.       }
  236.     else
  237.     if (strncmp(buffer,"MAGENTA",7) == 0)
  238.       {
  239.        for(i=0;i<=6;buffer[i++]=BLANK) ;
  240.        sprintf(colors1,"%c%s",ESCAPE,"[35m");
  241.       }
  242.     else
  243.     if (strncmp(buffer,"CYAN",4) == 0)
  244.       {
  245.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  246.        sprintf(colors1,"%c%s",ESCAPE,"[36m");
  247.       }
  248.     else
  249.     if (strncmp(buffer,"WHITE",5) == 0)
  250.       {
  251.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  252.        sprintf(colors1,"%c%s",ESCAPE,"[37m");
  253.       }
  254.     else
  255.        sprintf(colors1,"%c%s",ESCAPE,"[0m");
  256.  
  257.     leftadj(buffer) ;
  258.     if (buffer[0] == NULL) 
  259.        {
  260.         sprintf(colors2,"%c%s",ESCAPE,"[40m");
  261.         goto exitit;
  262.        }
  263.  
  264.     /* background colors */
  265.     if (strncmp(buffer,"BLACK",5) == 0)
  266.       {
  267.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  268.        sprintf(colors2,"%c%s",ESCAPE,"[40m");
  269.       }
  270.     else
  271.     if (strncmp(buffer,"RED",3) == 0)
  272.       {
  273.        for(i=0;i<=2;buffer[i++]=BLANK) ;
  274.        sprintf(colors2,"%c%s",ESCAPE,"[41m");
  275.       }
  276.     else
  277.     if (strncmp(buffer,"GREEN",5) == 0)
  278.       {
  279.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  280.        sprintf(colors2,"%c%s",ESCAPE,"[42m");
  281.       }
  282.     else
  283.     if (strncmp(buffer,"YELLOW",6) == 0)
  284.       {
  285.        for(i=0;i<=5;buffer[i++]=BLANK) ;
  286.        sprintf(colors2,"%c%s",ESCAPE,"[43m");
  287.       }
  288.     else
  289.     if (strncmp(buffer,"BLUE",4) == 0)
  290.       {
  291.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  292.        sprintf(colors2,"%c%s",ESCAPE,"[44m");
  293.       }
  294.     else
  295.     if (strncmp(buffer,"MAGENTA",7) == 0)
  296.       {
  297.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  298.        sprintf(colors2,"%c%s",ESCAPE,"[45m");
  299.       }
  300.     else
  301.     if (strncmp(buffer,"CYAN",4) == 0)
  302.       {
  303.        for(i=0;i<=3;buffer[i++]=BLANK) ;
  304.        sprintf(colors2,"%c%s",ESCAPE,"[46m");
  305.       }
  306.     else
  307.     if (strncmp(buffer,"WHITE",5) == 0)
  308.       {
  309.        for(i=0;i<=4;buffer[i++]=BLANK) ;
  310.        sprintf(colors2,"%c%s",ESCAPE,"[47m");
  311.       }
  312.  
  313.  exitit: ;
  314.     strcpy(colors,colors0) ;
  315.     strcat(colors,colors1) ;
  316.     strcat(colors,colors2) ;
  317.  
  318.     leftadj(buffer) ;
  319.     if (buffer[0] != NULL)   return(-1) ;
  320.     else return(0) ;
  321.    }
  322.  
  323. ansicls()
  324.  {   printf("%c%s",'\033',"[2J") ;     }
  325.  
  326. /* MAKECOL                                                           */
  327. /* this function converts a IBM video ram attribute to a character   */
  328. /* string of the correct attributes for use with the ansicol         */
  329. /* function                                                          */
  330. makecol(attrib,colors)
  331.   unsigned char attrib,colors[];
  332.  {
  333.    int i,j,back,fore,mod;
  334.    unsigned char cback[30],cfore[30],cintens[30];
  335.  
  336.    i = attrib ;
  337. /***if (i > 127) i -= 128****/
  338.    if (i > 127) i -= 128;
  339.    i = i / 16;
  340.    back = i;
  341.    if (back == 0)
  342.       strcpy(cback,"black");  else
  343.    if (back == 1)
  344.       strcpy(cback,"blue");  else
  345.    if (back == 2)
  346.       strcpy(cback,"green");  else
  347.    if (back == 3)
  348.       strcpy(cback,"cyan");  else
  349.    if (back == 4)
  350.       strcpy(cback,"red");  else
  351.    if (back == 5)
  352.       strcpy(cback,"magenta"); else
  353.    if (back == 6)
  354.       strcpy(cback,"yellow");
  355.    else
  356.       strcpy(cback,"white");
  357.  
  358.    cintens[0] = NULL ;
  359.    mod = attrib % 16;
  360.    fore = mod;
  361.    if (fore < 8)
  362.       strcpy(cintens,"low ");
  363.    else
  364.       strcpy(cintens,"high ");
  365.  
  366.    if ((fore == 0) || (fore == 8))
  367.       strcpy(cfore,"black"); else
  368.    if ((fore == 1)  || (fore == 9))
  369.       strcpy(cfore,"blue"); else
  370.    if ((fore == 2)  || (fore == 10))
  371.       strcpy(cfore,"green");  else
  372.    if ((fore == 3) || (fore == 11))
  373.       strcpy(cfore,"cyan");  else
  374.    if ((fore == 4) || (fore == 12))
  375.       strcpy(cfore,"red");  else
  376.    if ((fore == 5) || (fore == 13))
  377.       strcpy(cfore,"magenta"); else
  378.    if ((fore == 6) || (fore == 14))
  379.       strcpy(cfore,"yellow");
  380.    else
  381.       strcpy(cfore,"white");
  382.  
  383.  /* special cases */
  384.  
  385.     if (( (attrib-9) % 16) == 0)
  386.        strcat(cintens,"under ");
  387.     if (( (attrib-1) % 16) == 0)
  388.        strcat(cintens,"under ");
  389.     if (attrib > 127)
  390.        strcat(cintens,"blink ");
  391.  
  392.  
  393.   strcpy(colors,cintens);
  394.   strcat(colors,cfore);
  395.   strcat(colors," ");
  396.   strcat(colors,cback);
  397.  
  398.   return(0) ;
  399.  } /* end of program */
  400.  
  401. /* leftadj  - left adjust to position zero            */
  402. leftadj(s)
  403.   unsigned char s[];
  404.   {
  405.     int i,j,started;
  406.     started = 0 ;
  407.     for(i=0,j=0;i<=32000;i++)
  408.        {
  409.          if (s[i] == NULL) break ;
  410.          if (started)
  411.             s[j++] = s[i];
  412.          else
  413.            if (s[i] != BLANK)
  414.               {
  415.                 started = 1 ;
  416.                 s[j++] = s[i];
  417.               }
  418.        }
  419.     s[j]= NULL ;
  420.   }
  421.  
  422. getdata(line,col,length,colors,cdata)
  423.    int line,col,length;
  424.    unsigned char cdata[],colors[];
  425.   {
  426.    fgets(cdata,length,stdin);
  427.    return(0) ;
  428.   }
  429.